package pre.lxm.demo.chart.lbn;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import pre.lxm.demo.chart.DisplayUtil;

/**
 * Created by lbn on 2017/1/5.
 */

public class ChartView extends View {

    private static final String TAG = "ChartView";

    private int mWidth, mHeight;
    private int itemHeight;
    private int padding;
    private int itemWidth;
    private int poorValue;
    private float textSize;
    private float textStrokeWidth;
    private List<ChartItem> data;
    private Point[] mPoint;
    private Paint mPaint;
    private TextPaint mTextPaint;
    private int pointSize = 8;
    private int pointColor = Color.parseColor("#313335");
    private int valueMax = -1, valueMin = -1;


    public ChartView(Context context) {
        this(context, null);
    }

    public ChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setData();
        init();
    }

    private void init() {
        padding = DisplayUtil.getScreenWidth(getContext()) / 15;
        itemWidth = DisplayUtil.getScreenWidth(getContext()) / 8;
        mWidth = padding * 2 + itemWidth * data.size();
        mPoint = new Point[data.size()];
        for (int i = 0; i < mPoint.length; i++) {
            mPoint[i] = new Point();
            mPoint[i].x = itemWidth / 2 + itemWidth * i;
        }
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(textStrokeWidth);
        mPaint.setColor(pointColor);
        mPaint.setStyle(Paint.Style.FILL);
    }

    /**
     * 初始化文字画笔
     */
    private void initTextPaint() {
        mTextPaint = new TextPaint();
        mTextPaint.setTextSize(textSize);
        mTextPaint.setStrokeWidth(textStrokeWidth);
        mTextPaint.setColor(pointColor);
        mTextPaint.setStyle(Paint.Style.FILL);
    }

    /**
     * 测量高度、初始化画笔、并重新设置View的宽
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mHeight = getMeasuredHeight() - padding;
        itemHeight = mHeight / 7;
        textSize = itemHeight / 2.8f;
        textStrokeWidth = itemHeight / 8;
        pointSize = mHeight/20;
        initPaint();
        initTextPaint();
        setMeasuredDimension(mWidth, getMeasuredHeight());
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(0, padding);
        onDrawLeftText(canvas);
        onDrawPoint(canvas);
        onDrawBottomText(canvas);
    }

    /**
     * 左边文字
     * @param canvas
     */
    private void onDrawLeftText(Canvas canvas) {

        for (ChartItem item : data) {
            if (valueMax < item.value) {
                valueMax = item.value;
            }

            if (valueMin == -1 || valueMin > item.value) {
                valueMin = item.value;
            }
        }
        if (valueMax <= 130) {
            valueMax = 130;
        } else {
            valueMax = (valueMax / 10 + 1) * 10;
        }
        if (valueMin >= 70) {
            valueMin = 70;
        } else {
            valueMin = (valueMin / 10 - 1) * 10;
        }


        poorValue = (valueMax - valueMin) / 6;
        float textWidth = mPaint.measureText(String.valueOf(valueMax));
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        for (int i = 0; i < 7; i++) {
            int baseY = (int) ((mTextPaint.descent() + mTextPaint.ascent()) / 2);
            canvas.drawText(String.valueOf(valueMax - poorValue * i), 0, itemHeight * i - baseY, mTextPaint);

            /**
             * 横线测试用
             */
//            mPaint.setStrokeWidth(2);
//            canvas.drawLine(0, itemHeight * i, canvas.getWidth(), itemHeight * i, mPaint);
        }
        canvas.translate(textWidth / 2, 0);
    }

    /**
     * 底部文字
     * @param canvas
     */
    private void onDrawBottomText(Canvas canvas) {
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        for (int i = 0; i < data.size(); i++) {
            canvas.drawText(String.valueOf(i + 1), mPoint[i].x, mHeight, mTextPaint);
            /**
             * 竖线测试用
             */
//            canvas.drawLine(mPoint[i].x, mHeight, mPoint[i].x, 0, mPaint);
        }
    }

    /**
     * 画点和线
     * @param canvas
     */
    private void onDrawPoint(Canvas canvas) {
//        int radius = DisplayUtil.dip2px(getContext(), pointSize / 2);
        int radius = pointSize / 2;
        for (int i = 0; i < data.size(); i++) {
            ChartItem item = data.get(i);
            float y = (1f - ((float) item.value / (float) valueMax)) * (mHeight * (((float) valueMax - poorValue * 6f) / valueMax) + mHeight);
            Log.e(TAG, "y:" + String.valueOf(y));
            canvas.drawCircle(mPoint[i].x, y, radius, mPaint);
            mPoint[i].y = (int) y;
            mPaint.setStrokeWidth(2);
            if (i > 0) {
                canvas.drawLine(mPoint[i - 1].x, mPoint[i - 1].y, mPoint[i].x, mPoint[i].y, mPaint);
            }
        }
    }



    /**
     * 测试用数据
     */
    private void setData() {
        if (data == null) {
            data = new ArrayList<>();
        }
        List<ChartItem> list = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 20; i++) {
            ChartItem item = new ChartItem();
            item.date = i;
            item.value = r.nextInt(130 - 60) + 60;
            list.add(item);
        }
        data.addAll(list);
    }

}
